package com.inkyi.assistant.app.todolist.table;

import com.inkyi.assistant.app.todolist.TodoContext;
import com.inkyi.assistant.app.todolist.event.TodoEvent;
import com.inkyi.assistant.app.todolist.event.TodoRecordDTO;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class TodoTableModelListener implements TableModelListener {


    @Override
    public void tableChanged(TableModelEvent e) {
        TodoTableModel sourceModel = (TodoTableModel) e.getSource();
        if (e.getType() == TableModelEvent.UPDATE) {
            int firstRow = e.getFirstRow();
            int column = e.getColumn();
            // 完成列
            if (column == 1) {
                TodoItem todoItem = sourceModel.getValueByRow(firstRow);
                TodoRecordDTO dto = new TodoRecordDTO();
                dto.setId(todoItem.getId());
                dto.setTitle(todoItem.getTitle());
                dto.setFinish(todoItem.getFinish());
                TodoContext.getEventWrap().fireActionEvent(TodoEvent.of(e.getSource(), TodoEvent.TYPE_RECORD_FINISH, dto));
            }
            // 内容列
            else if (column == 2) {
                TodoItem todoItem = sourceModel.getValueByRow(firstRow);
                TodoRecordDTO dto = new TodoRecordDTO();
                dto.setId(todoItem.getId());
                dto.setTitle(todoItem.getTitle());
                dto.setFinish(todoItem.getFinish());
                TodoContext.getEventWrap().fireActionEvent(TodoEvent.of(e.getSource(), TodoEvent.TYPE_RECORD_SAVE, dto));
            }
        }
    }
}
